package com.st.sbmb.controller.rmqcontroller;

import com.st.sbmb.config.rmqconfig.TopicExchangeConfig;
import com.st.sbmb.mapper.UserMapper;
import com.st.sbmb.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    private UserMapper userMapper;

    private final RabbitTemplate rabbitTemplate;

    public TopicController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @GetMapping("send")
    public Object sendMsg(String routingKey) {
        int id = 1;
        User user = userMapper.findUserById(id);
        rabbitTemplate.convertAndSend(TopicExchangeConfig.TOPIC_EXCHANGE, routingKey, user.toString());
        /*rabbitTemplate.convertAndSend(TopicExchangeConfig.TOPIC_EXCHANGE, routingKey, "发送一条测试消息：topic" + user);*/
        return "topic消息发送成功！！";
    }


}
